package com.financing.platform.web;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.financing.platform.dto.FaUserAuthDTO;
import com.financing.platform.dto.FaUserCertDTO;
import com.financing.platform.dto.FaUserDTO;
import com.financing.platform.service.IFaUserAuthService;
import com.financing.platform.service.IFaUserService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.Dict;

@Controller
public class FAUserAction extends BaseAction{
	
	@Autowired
	IFaUserService faUserServiceImpl;
	
	@Autowired
	IFaUserAuthService faUserAuthServiceImpl;
	
	@RequestMapping("/queryFaUserListPer.shtml")
	public ModelAndView queryFaUserListPer(HttpServletRequest request,
			HttpServletResponse response) {
			request.getSession().setAttribute(Dict.requestID, "queryFaUserListPer.shtml");
			return new ModelAndView("faUserPer");
	}
	
	@RequestMapping("/queryFaUserList.shtml")
	public ModelAndView queryFaUserList(HttpServletRequest request,
			HttpServletResponse response,FaUserDTO faUserDTO) {
			log.info("Begin query fa user list!");
			List<FaUserDTO> faUserList = faUserServiceImpl.queryAllFaUserInfo(faUserDTO);
			Map<String,Object> faUserMap = new HashMap<String,Object>();
			faUserMap.put("faUserList", faUserList);
			
			request.getSession().setAttribute(Dict.faUserDTO, faUserDTO);
			log.info("End query fa user list!");
			return new ModelAndView("faUserList",faUserMap);
	}
	
	@RequestMapping("/queryFaUser.shtml")
	public ModelAndView queryFaUser(HttpServletRequest request,
			HttpServletResponse response,FaUserDTO faUserDTO) {
		
		    log.info("Begin query fa user!");
		    
		    //查询FA信息
		    faUserDTO = faUserServiceImpl.queryFaUserInfo(faUserDTO);
		    
		    FaUserAuthDTO faUserAuthDTO = new FaUserAuthDTO();
		    faUserAuthDTO.setFaId(faUserDTO.getId());
		    
		    //查询FA证书信息
		    List<FaUserAuthDTO> faUserAuthList = faUserAuthServiceImpl.queryFaUserAuthList(faUserAuthDTO);
		    
		    FaUserCertDTO faUserCertDTO = new FaUserCertDTO();
		    faUserCertDTO.setFaId(faUserDTO.getId());
		    
		    List<FaUserCertDTO> faUserCertList = faUserAuthServiceImpl.queryFaUserCertList(faUserCertDTO);
		    
		    Map<String,Object> faUserMap = new HashMap<String,Object>();
		    faUserMap.put("faUserInfo", faUserDTO);
		    faUserMap.put("faUserAuthList", faUserAuthList);
		    faUserMap.put("faUserCertList", faUserCertList);
		    
		    log.info("End query fa user !");
			return new ModelAndView("faUserModify",faUserMap);
	}
	
	@RequestMapping("/updateFaUser.shtml")
	public ModelAndView updateFaUser(HttpServletRequest request,
			HttpServletResponse response,FaUserDTO faUserDTO) {
			log.info("Begin update fa user info !");
			
			DTOdateSet.setUpdateInfo(faUserDTO, request.getSession());
			
			int total = 0;
			try {
				total = faUserServiceImpl.updateFaUserInfo(faUserDTO);
			} catch (Exception e) {
				log.error("Update fa user error!",e);
			}
			
			Map<String,Object> resultMap = new HashMap<String,Object>();
			resultMap.put("result", total);
			
			log.info("End update fa user info !");
			return new ModelAndView("faUserModifyResult",resultMap);
	}
}
